package com.zh.work.controller.platform.channel;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.constant.ChannelConstants;
import com.zh.work.dao.platform.channel.OnlineChannelMapper;
import com.zh.work.entity.channel.OnlineChannelEntity;
import com.zh.work.service.platform.channel.OnlineChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 网关渠道管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/05
 * @Version: 1.0.1
 */
@Api(value = "网关渠道管理控制器", produces = "application/json", tags = {"网关渠道管理控制器"})
@RestController
@RequestMapping("/onlineChannel")
public class OnlineChannelController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/05
     */
    @Autowired
    private OnlineChannelMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/05
     */
    @Autowired
    private OnlineChannelService service;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/05
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增一条记录", tags = {"putAddOnlineChannel"}, notes = "新增一条记录")
    @PutMapping("/putAddOnlineChannel")
    @ResponseBody
    public JSONResult<Object> putAddOnlineChannel(@ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity")
                                                  @RequestBody OnlineChannelEntity onlineChannelEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putOnlineChannel(onlineChannelEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1)
                    jsonResult.setMessage("添加网关机构外键信息出错!");
                else if (i == -2)
                    jsonResult.setMessage("添加网关信息出错!");
                else
                    jsonResult.setMessage("系统错误");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModOnlineChannelById"}, notes = "通过id修改信息")
    @PutMapping("/putModOnlineChannelById")
    @ResponseBody
    public JSONResult<Object> putModOnlineChannelById(@ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity")
                                                      @RequestBody OnlineChannelEntity onlineChannelEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putModOnlineChannel(onlineChannelEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1)
                    jsonResult.setMessage("修改网关机构外键信息出错!");
                else
                    jsonResult.setMessage("修改网关信息出错!");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteOnlineChannelById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteOnlineChannelById")
    public JSONResult<Object> deleteOnlineChannelById(@ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity") OnlineChannelEntity onlineChannelEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (onlineChannelEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteOnlineChannelBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteOnlineChannelBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteOnlineChannelBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody @RequestParam("ids") List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getOnlineChannelById"}, notes = "根据id查询详细信息")
    @GetMapping("/getOnlineChannelById")
    public JSONResult<OnlineChannelEntity> getOnlineChannelById(@ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity")
                                                                            OnlineChannelEntity onlineChannelEntity) {
        JSONResult<OnlineChannelEntity> jsonResult = new JSONResult<>();
        try {
            OnlineChannelEntity result = onlineChannelEntity.selectById(onlineChannelEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesOnlineChannels"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesOnlineChannels")
    public JSONResult<List<OnlineChannelEntity>> getPagesOnlineChannels(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity") OnlineChannelEntity onlineChannelEntity) {
        JSONResult<List<OnlineChannelEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<OnlineChannelEntity> result = this.service.selectOnlineChannelPages(new Page<>(pageNo, pageSize), onlineChannelEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据渠道编号获取机构编号", tags = {"getBranchNoByOnlineNo"}, notes = "根据渠道编号获取机构编号")
    @GetMapping("/getBranchNoByOnlineNo")
    public JSONResult<Map> getBranchNoByOnlineNo(@ApiParam(value = "渠道编号", name = "channelCode") @RequestParam String channelCode) {
        JSONResult<Map> jsonResult = new JSONResult<>();
        try {
            Map map = mapper.getBranchNoByOnlineNo(channelCode);
            if (map != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "修改网关渠道状态", tags = {"putModOnlineStatus"}, notes = "修改网关渠道状态")
    @PutMapping("putModOnlineStatus")
    @ResponseBody
    public JSONResult<Object> putModOnlineStatus(@ApiParam(value = "网关渠道实体类", name = "onlineChannelEntity")
                                                 @RequestBody OnlineChannelEntity onlineChannelEntity) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            int i = this.mapper.updateById(onlineChannelEntity);
            if (i > 0)
                jsonResult.setSuccess(true);
            else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请检查状态设置是否正确!");
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取渠道信息", tags = {"getOnlineChannel"}, notes = "获取渠道信息")
    @GetMapping("/getOnlineChannel")
    public JSONResult<List<OnlineChannelEntity>> getOnlineChannel() {
        JSONResult<List<OnlineChannelEntity>> jsonResult = new JSONResult<>();
        try {
            List<OnlineChannelEntity> list = JSON.parseArray(JSON.toJSONString(redisTemplate.opsForValue()
                    .get(ChannelConstants.ONLINE_CHANNEL_CACHE)), OnlineChannelEntity.class);
            if (Objects.isNull(list)) {
                list = mapper.selectList(new LambdaQueryWrapper<OnlineChannelEntity>()
                        .select(OnlineChannelEntity::getChannelName, OnlineChannelEntity::getChannelCode)
                .eq(OnlineChannelEntity::getStatus,1));
            }
            jsonResult.setCode(200);
            jsonResult.setMessage("查询完成");
            jsonResult.setSuccess(true);
            jsonResult.setData(list);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


}
